package com.jysz.certificate.web.action.cm.update;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.constant.OperCodeConstant;
import com.jysz.certificate.persistence.enums.CertificateStatus;
import com.jysz.certificate.persistence.enums.WorkflowOperateType;
import com.jysz.certificate.persistence.enums.WorkflowState;
import com.jysz.certificate.persistence.enums.WorkflowType;
import com.jysz.certificate.persistence.model.cm.Certificate;
import com.jysz.certificate.persistence.model.cm.Certification;
import com.jysz.certificate.persistence.model.cm.CertificationDTO;
import com.jysz.certificate.persistence.model.system.Brand;
import com.jysz.certificate.persistence.model.system.Financial;
import com.jysz.certificate.service.cm.CertificateService;
import com.jysz.certificate.service.cm.CertificationService;
import com.jysz.certificate.service.system.BrandService;
import com.jysz.certificate.service.system.FinancialService;
import com.jysz.certificate.service.workflow.WorkflowService;
import com.jysz.webcore.ssh.annotation.PermissionPoint;

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/cm/update")
public class CertificateAction extends AbstractBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	private BrandService brandService;// 主机厂service
	@Resource
	private FinancialService financialService;// 金融机构service
	
	@Resource
	private CertificationService cfService;// 合格证核心Service
	
	@Resource
	private WorkflowService workflowService;// 流程
	
	@Resource
	private CertificateService certificateService;
	
	private CertificationDTO search;
	private Certificate c;
	private Certification cer;
	private Long id;
	private String msg;
	private int status;
	private List<Financial> financials;
	private List<Brand> brandList;
	
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	@Action(INDEX)
	public String index(){
		brandList = brandService.findAll();
		financials = financialService.finList();
		this.setPage(certificateService.findPage(search, getCurrentPage(), getPageSize(),status));
		return "index";
	}
	
	@Action("toExamine")
	@PermissionPoint(point=OperCodeConstant.CM_UPDATE_RECORD_AUDIT)
	public String toExamine(){
		c = certificateService.getUpdateCertificate(id,CertificateStatus.SUBMITED);
		cer = cfService.getCertification(id);
		setRedirectUrl("index.do?currentPage=" + getCurrentPage(), "search",search);
		return "examine";
	}
	
	@Action("workToExamine")
	@PermissionPoint(point=OperCodeConstant.CM_UPDATE_RECORD_AUDIT)
	public String workToExamine(){
		c = certificateService.find(id);
		cer = cfService.getCertification(c.getCertification().getId());
		setRedirectUrl("index.do?currentPage=" + getCurrentPage(), "search",null);
		return "examine";
	}
	
	@Action("toDetail")
	public String toDetail(){
		c = certificateService.get(id);
		cer = cfService.getCertification(c.getCertification().getId());
		setRedirectUrl("index.do?currentPage=" + getCurrentPage(), "search",search);
		return "detail";
	}
	
	@Action("updatePass")
	@PermissionPoint(point=OperCodeConstant.CM_UPDATE_RECORD_AUDIT)
	public String updatePass() {
		c = certificateService.getCertificateById(c.getId());
		c.setStatus(CertificateStatus.FINISH);
		workflowService.audit(getUser(),
				workflowService.find(WorkflowType.UPDATE, c.getId()),
				WorkflowState.FINISH, WorkflowOperateType.PASS, msg);
		setSuccessPopMessage("审核通过");
		c.setRemark(msg);
		certificateService.save(c);
		cer = cfService.getCertification(c.getCertification().getId());
		cer = beanCopy(cer, c);
		cfService.save(cer);
		return SHOW_MSG_PAGE;
	}
	
	public Certification beanCopy(Certification cer,Certificate c){
		cer.setBrand(brandService.selete(c.getBrand().getId()));
		cer.setFinancial(financialService.getFinancial(c.getFinancial().getId()));
		cer.setDraft(c.getDraft());
		cer.setOrderNumber(c.getOrderNumber());
		cer.setMotorcycleType(c.getMotorcycleType());
		cer.setGoingOutDate(c.getGoingOutDate());
		cer.setChassis(c.getChassis());
		cer.setScheduledDate(c.getScheduledDate());
		cer.setCertificationNum(c.getCertificationNum());
		cer.setSupplyPrice(c.getSupplyPrice());
		cer.setDraftBeginDate(c.getDraftBeginDate());
		cer.setDraftEndDate(c.getDraftEndDate());
		return cer;
	}

	@Action("updateBack")
	@PermissionPoint(point=OperCodeConstant.CM_UPDATE_RECORD_AUDIT)
	public String updateBack() {
		c = certificateService.getCertificateById(c.getId());
		c.setStatus(CertificateStatus.REFUSED);
		workflowService.audit(getUser(),
				workflowService.find(WorkflowType.UPDATE, c.getId()),
				WorkflowState.FINISH, WorkflowOperateType.REFUSE, msg);
		c.setRemark(msg);
		setSuccessPopMessage("审核不通过");
		certificateService.save(c);
		return SHOW_MSG_PAGE;
	}
	
	@PermissionPoint(point = OperCodeConstant.CM_UPDATE_RECORD_DELETE)
	@Action(DESTORY)
	public String destory(){
		certificateService.destoryAndWorkFlow(id);
		setSuccessPopMessage("删除数据成功");
		if(search != null){
			StringBuffer sb = new StringBuffer();
			sb.append("index.do?currentPage="+getCurrentPage());
			setRedirectUrl(sb.toString());
		}else{
			setRedirectUrl("index.do?currentPage="+getCurrentPage());
		}
		return SHOW_MSG_PAGE;
	}
	
	public CertificationDTO getSearch() {
		return search;
	}
	public void setSearch(CertificationDTO search) {
		this.search = search;
	}
	public Certificate getC() {
		return c;
	}
	public void setC(Certificate c) {
		this.c = c;
	}
	public Certification getCer() {
		return cer;
	}
	public void setCer(Certification cer) {
		this.cer = cer;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public List<Financial> getFinancials() {
		return financials;
	}

	public void setFinancials(List<Financial> financials) {
		this.financials = financials;
	}

	public List<Brand> getBrandList() {
		return brandList;
	}

	public void setBrandList(List<Brand> brandList) {
		this.brandList = brandList;
	}

}
